import http from '@/utils/axios'
import { AxiosPromise } from 'axios'
import axios from 'axios'
export interface LoginParamsType {
  username: string
  password: string
}

export interface OauthType {
  refresh_token: string
  access_token: string
  token_type: string
  expires_at: number
}

/**
 * 获取token
 * @param params
 * @returns { AxiosPromise }
 */
export const getToken = async (params) => {
  //密码转为base64编码
  const encode = encodeURI(params.password)
  params.password = btoa(encode)
  const response = await axios({
    url: '/oauth/oauth/token',
    method: 'POST',
    params: {
      client_id: 'tplatformmanagementfront',
      client_secret: '9up552uab8jt1q9vi10yggq6srle9alkwnw3lnigr3r2c45z8ogotxmhm0mztqx4',
      grant_type: 'ldap',
      ...params,
    },
  })
  return response.data
}
/**
 * 获取用户信息
 * @param access_token
 * @returns { AxiosPromise }
 */
export const getUserInfo = (access_token: string): any => {
  return http({
    url: '/oauth/oauth/check_token',
    method: 'POST',
    params: { token: access_token },
  })
}

/**
 * 刷新token
 * @param params
 * @returns { AxiosPromise }
 */
export const refreshToken = async (refreshToken: string) => {
  const response = await axios({
    url: '/oauth/oauth/token',
    method: 'POST',
    params: {
      client_id: 'tplatformmanagementfront',
      client_secret: '9up552uab8jt1q9vi10yggq6srle9alkwnw3lnigr3r2c45z8ogotxmhm0mztqx4',
      grant_type: 'refresh_token',
      refresh_token: refreshToken,
    },
  })
  return response.data
}
export const refreshToken2 = (refreshToken: string) => {
  return axios({
    url: '/oauth/oauth/token',
    method: 'POST',
    params: {
      client_id: 'tplatformmanagementfront',
      client_secret: '9up552uab8jt1q9vi10yggq6srle9alkwnw3lnigr3r2c45z8ogotxmhm0mztqx4',
      grant_type: 'refresh_token',
      refresh_token: refreshToken,
    },
  })
}
